package com.imooc.mall.model.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.imooc.mall.model.dao.CategoryMapper;
import com.imooc.mall.model.exception.ImoocMallException;
import com.imooc.mall.model.exception.ImoocMallExceptionEnum;
import com.imooc.mall.model.pojo.Category;
import com.imooc.mall.model.request.AddCategoryReq;
import com.imooc.mall.model.service.CategoryService;
import com.imooc.mall.model.vo.CategoryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;


import java.util.ArrayList;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    CategoryMapper categoryMapper;

    @Override
    public void add(AddCategoryReq addCategoryReq){
        Category category=new Category();
         //相同字段，类型，复制
        BeanUtils.copyProperties(addCategoryReq,category);
//        名字不能相同
        Category categoryOld = categoryMapper.selectByName(category.getName());
        if(categoryOld!=null){
            throw new ImoocMallException(ImoocMallExceptionEnum.NEED_EXITSTED);
        }
//        插入成功则返回1，失败返回0
        int count = categoryMapper.insertSelective(category);
        if(count==0){
            throw new ImoocMallException(ImoocMallExceptionEnum.CREATE_FAILED);
        }

    }

    @Override
    public void update(Category updateCategory){
        if(updateCategory.getName()!=null){
            //查找是否重名
            Category categoryOld = categoryMapper.selectByName(updateCategory.getName());
            //重名了，但有的时候只是没修改名字,所以在看是否id相同，如果id相同则不报错
            if(categoryOld!=null && !categoryOld.getId().equals(updateCategory.getId())){
               throw new ImoocMallException(ImoocMallExceptionEnum.NEED_EXITSTED);
            }
        }
        int count = categoryMapper.updateByPrimaryKeySelective(updateCategory);
        if(count==0){
            throw new ImoocMallException(ImoocMallExceptionEnum.UPDATE_FAILED);
        }
    }

    @Override
    public void delete(Integer id){
        Category categoryOld = categoryMapper.selectByPrimaryKey(id);
        if(categoryOld==null){
            throw new ImoocMallException(ImoocMallExceptionEnum.DELETE_FAILED);
        }
        int count = categoryMapper.deleteByPrimaryKey(id);
        if(count==0){
            throw new ImoocMallException(ImoocMallExceptionEnum.DELETE_FAILED);
        }
    }

    @Override
    public PageInfo listForAdmin(Integer pageNum,Integer pageSize){
        //"type,order_num"  先对type进行排序，同类型的type，用order_num进行排序
        PageHelper.startPage(pageNum,pageSize,"type,order_num");
        List<Category> categoryList = categoryMapper.selectList();
        //pageinfo 是最后返回给前端的一个类型
        PageInfo<Category> pageInfo = new PageInfo<>(categoryList);
        return pageInfo;
    }

    @Override
    @Cacheable(value ="listForCustomer" )
    public List<CategoryVo> listForCustomer(Integer parnetId){
        ArrayList<CategoryVo> categoryVoArrayList=new ArrayList<>();
        recursivelyFindCategoried(categoryVoArrayList,parnetId);
        return categoryVoArrayList;
    }

    public void recursivelyFindCategoried(List<CategoryVo> categoryVoList,Integer parentId){
        List<Category> categoryList = categoryMapper.selectCategoriesByParentId(parentId);
        if(!CollectionUtils.isEmpty(categoryList)){
            for (int i = 0; i < categoryList.size(); i++) {
                Category category = categoryList.get(i);
                CategoryVo categoryVo=new CategoryVo();
                BeanUtils.copyProperties(category,categoryVo);
                categoryVoList.add(categoryVo);
                recursivelyFindCategoried(categoryVo.getChildCategory(),categoryVo.getId());
            }

        }

    }

}
